package com.hk.service.impl.kyc;

import com.hk.commons.StringHelper;
import com.hk.commons.utils.sys.DateHelper;
import com.hk.service.order.IOrderContract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.HqlFilter;
import com.hk.commons.LongId;
import com.hk.commons.entity.Answer;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.sys.IAnswer;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class AnswerImpl extends BaseServiceImpl<Answer> implements IAnswer {

	private Logger logger = LoggerFactory.getLogger(AnswerImpl.class);


	@Autowired
	private IOrderContract orderContractService;


	@Override
	public Answer findById(Long id) {
		return getById(id);
	}

	@Override
	public Answer getByUserId(Long userId) {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("userId", userId);
		return getByFilter(hqlFilter);
	}

	@Override
	public Long add(Answer model) {
		// TODO Auto-generated method stub
		long id = LongId.get();
		model.setId(id);
		model.setCts(System.currentTimeMillis());
		super.save(model);

		this.updKycUrl(model.getUserId());

		return model.getId();
	}

	public Answer delete(Long id) {
		Answer model = new Answer();
		model.setId(id);
		delete(model);
		return model;
	}

	@Override
	public Answer getByMobileId(String mobileId) {
		// TODO Auto-generated method stub
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("mobileId", mobileId);
		return getByFilter(hqlFilter);
	}

	@Override
	public Boolean checkByUserId(Long userId) {
		logger.info("start to check the user kyc result , userId={}", userId);
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("userId", userId);
		Answer answer = getByFilter(hqlFilter);

		if( answer == null){
			logger.info("user not finish kyc userId ={}", userId);
			return false;
		}else{
			return true;
		}


	}

	// 获取时间差 判断和现在相比是否超过一年
	@Override
	public  boolean moreThanOneYear(long  userId) {
		logger.info("start to check the user kyc result , userId={}", userId);
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("userId", userId);
		Answer answer = getByFilter(hqlFilter);

		long nowTime = System.currentTimeMillis();

		long oneYearTime = 31536000000l ;

		Boolean checkReslut =  (nowTime - answer.getCts() >= oneYearTime);

		if( checkReslut ){
			logger.info("user kyc has finished more than one year userId ={}", userId);
			return true;
		}else{
			return false;
		}


	}

	@Override
	public void updKycUrl(long userId){
		//调用多线程生成合同
		try {
			ExecutorService executor = Executors.newFixedThreadPool(1);
			Runnable task = new Runnable() {
				public void run() {
					String kycPdfUrl = orderContractService.getKycPdfUrl(userId);
					String kycHTMLUrl = orderContractService.getKycHTMLUrl(userId);
					Answer an =  getByUserId(userId);
					an.setKycPDFUrl(kycPdfUrl);
					an.setKycHTMLUrl(kycHTMLUrl);
					update(an);
				}
			};
			executor.execute(task);
			executor.shutdown();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
